package com.jhws.sys.cms.service;

import com.jhws.core.db.BaseDao;
import com.jhws.core.exception.BizException;
import com.jhws.sys.cms.bo.ResourceCustomType;
import com.jhws.sys.cms.bo.ResourceType;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResourceTypeService {
    @Resource
    private BaseDao BaseDao;


    /**
     * 条件查询类型
     *
     * @param where 条件
     * @return
     */
    public List<ResourceType> queryTypesByWhere(String where) {
        return BaseDao.findList("from ResourceType " + where);
    }

    /**
     * 保存自定义类型
     *
     * @param customType 自定义类型
     * @return
     */
    public String saveResourceCustomType(ResourceCustomType customType) {
        if (StringUtils.isNotBlank(customType.getCustomTypeId())) {
            BaseDao.saveOrUpdate(customType);
        } else {
            List result = BaseDao.j().queryForList("select 1 from cms_resource_custom_type where custom_type_code = ?", customType.getCustomTypeCode());
            if (result.size() > 0) {
                throw new BizException("分类编码已经存在！");
            }
            BaseDao.save(customType);
        }
        return customType.getCustomTypeId();
    }
}
